from litestar import Controller, get, post, put
from litestar.di import Provide
from litestar.repository import FilterTypes
from litestar.params import Dependency
from sqlalchemy.ext.asyncio import AsyncSession
# from domain.test1 import  Repository
from service.test1 import Service
from domain.test1 import WriteDTO, ReadDTO
from models.test1 import Test1Model
from typing import Any

__all__ = ["Test1Controller"]

def provides_service(db_session: AsyncSession) -> Service:
    """Constructs repository and service objects for the request."""
    return Service(session=db_session)

class Test1Controller(Controller):
    dto = WriteDTO
    return_dto = ReadDTO
    path = "/countries"
    dependencies = {"service": Provide(provides_service, sync_to_thread=False)}
    tags = ["Countries"]

    @get()
    async def get_countries(
        self, service: Service, filters: list[FilterTypes] = Dependency(skip_validation=True)
    ) -> list[Test1Model]:
        """Get a list of country."""
        return await service.list(*filters)
    
    @post()
    async def get_create(self, service: Service, data: Test1Model) -> Test1Model:
        """Create a new country."""
        return await service.create(data)

    @put()
    async def get_update(self, service: Service, id_: Any, data: Test1Model) -> Test1Model:
        """Update a country."""
        return await service.update(id_, data)